﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;
using EnvDTE80;

namespace SinacorRules
{
    public sealed class Rule_Suffix : BaseAddIn.Rules.BaseRuleAddIn
    {
        #region Campos

        Type _tipoAttribute;
        Type _tipoEventArgs;
        Type _tipoException;
        Type _tipoDictionary;
        Type _tipoDictionaryGeneric;
        Type _tipoIEnumerable;
        Type _tipoICollection;
        Type _tipoIList;
        Type _tipoIEnumerableGeneric;
        Type _tipoICollectionGeneric;
        Type _tipoIListGeneric;
        Type _tipoCodeAccessPermission;
        Type _tipoIPermission;

        #endregion

        #region Constructor

        public Rule_Suffix()
            : base("Suffix", "SinacorRules.SinacorRules", typeof(Rule_Suffix).Assembly)
        {
            _tipoAttribute = typeof(System.Attribute);
            _tipoEventArgs = typeof(System.EventArgs);
            _tipoException = typeof(System.Exception);
            _tipoDictionary = typeof(System.Collections.IDictionary);
            _tipoDictionaryGeneric = typeof(System.Collections.Generic.IDictionary<object, object>);
            _tipoIEnumerable = typeof(System.Collections.IEnumerable);
            _tipoICollection = typeof(System.Collections.ICollection);
            _tipoIList = typeof(System.Collections.IList);
            _tipoIEnumerableGeneric = typeof(System.Collections.Generic.IEnumerable<object>);
            _tipoICollectionGeneric = typeof(System.Collections.Generic.ICollection<object>);
            _tipoIListGeneric = typeof(System.Collections.Generic.IList<object>);
            _tipoCodeAccessPermission = typeof(System.Security.CodeAccessPermission);
            _tipoIPermission = typeof(System.Security.IPermission);
        }

        #endregion

        #region Protected Override Methods

        protected sealed override void Check(CodeClass2 classe)
        {
            if (Common.Rotinas.CheckClassInheritance(classe, _tipoAttribute)
                   && !classe.Name.EndsWith("Attribute"))
            {
                GeraProblemaAddIn(classe.StartPoint.Parent.Parent.FullName,
                                classe.StartPoint.Line,
                                classe.StartPoint.DisplayColumn,
                                "AttributeSuffixResolution",
                                classe.Name,
                                classe.Namespace.Name);
            }
            else if (Common.Rotinas.CheckClassInheritance(classe, _tipoEventArgs)
                   && !classe.Name.EndsWith("EventArgs"))
            {
                GeraProblemaAddIn(classe.StartPoint.Parent.Parent.FullName,
                                classe.StartPoint.Line,
                                classe.StartPoint.DisplayColumn,
                                "EventArgsSuffixResolution",
                                classe.Name,
                                classe.Namespace.Name);
            }
            else if (Common.Rotinas.CheckClassInheritance(classe, _tipoException)
               && !classe.Name.EndsWith("Exception"))
            {
                GeraProblemaAddIn(classe.StartPoint.Parent.Parent.FullName,
                                   classe.StartPoint.Line,
                                   classe.StartPoint.DisplayColumn,
                                   "ExceptionSuffixResolution",
                                   classe.Name,
                                   classe.Namespace.Name);
            }
            else if (Common.Rotinas.CheckInterfaceInheritance(classe,
                                                              _tipoDictionary,
                                                              _tipoDictionaryGeneric)
                     && !classe.Name.EndsWith("Dictionary"))
            {
                GeraProblemaAddIn(classe.StartPoint.Parent.Parent.FullName,
                                   classe.StartPoint.Line,
                                   classe.StartPoint.DisplayColumn,
                                   "DictionarySuffixResolution",
                                   classe.Name,
                                   classe.Namespace.Name);
            }
            else if (Common.Rotinas.CheckInterfaceInheritance(classe,
                                                             _tipoIEnumerable,
                                                             _tipoICollection,
                                                             _tipoIList,
                                                             _tipoIEnumerableGeneric,
                                                             _tipoICollectionGeneric,
                                                             _tipoIListGeneric)
                    && !classe.Name.EndsWith("Collection"))
            {
                GeraProblemaAddIn(classe.StartPoint.Parent.Parent.FullName,
                                   classe.StartPoint.Line,
                                   classe.StartPoint.DisplayColumn,
                                   "CollectionSuffixResolution",
                                   classe.Name,
                                   classe.Namespace.Name);
            }
            else if ((Common.Rotinas.CheckClassInheritance(classe, _tipoCodeAccessPermission)
                     || Common.Rotinas.CheckInterfaceInheritance(classe, _tipoIPermission))
                    && !classe.Name.EndsWith("Permission"))
            {
                GeraProblemaAddIn(classe.StartPoint.Parent.Parent.FullName,
                                   classe.StartPoint.Line,
                                   classe.StartPoint.DisplayColumn,
                                   "PermissionSuffixResolution",
                                   classe.Name,
                                   classe.Namespace.Name);
            }
        }

        #endregion
    }
}
